package com.zh.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import co.elastic.clients.elasticsearch.xpack.usage.Base;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zh.common.BaseResponse;
import com.zh.common.ErrorCode;
import com.zh.common.ResultUtils;
import com.zh.domain.dto.task.AddTaskRequest;
import com.zh.domain.dto.task.PageTaskDynamicRequest;
import com.zh.domain.dto.task.PageTaskRequest;
import com.zh.domain.dto.task.UpdateTaskRequest;
import com.zh.domain.entity.Task;
import com.zh.domain.entity.TaskDynamics;
import com.zh.domain.vo.requirement.RequirementVo;
import com.zh.domain.vo.task.TaskDynamicVo;
import com.zh.domain.vo.task.TaskVo;
import com.zh.exception.ThrowUtils;
import com.zh.job.TaskManager;
import com.zh.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "TaskController", value = "任务控制器")
@RestController
@RequestMapping("/task")
@Slf4j
@SaCheckLogin
public class TaskController {


    private final TaskService taskService;
    private final UserService userService;
    @Lazy
    private final ProjectService projectService;

    private final ProjectUserService projectUserService;

    private final ProjectDynamicsService projectDynamicsService;

    private final MessageService messageService;

    public TaskController(TaskService taskService,
                          UserService userService,
                          ProjectService projectService,
                          ProjectUserService projectUserService,
                          ProjectDynamicsService projectDynamicsService,
                          MessageService messageService) {
        this.taskService = taskService;
        this.userService = userService;
        this.projectService = projectService;
        this.projectUserService = projectUserService;
        this.projectDynamicsService = projectDynamicsService;
        this.messageService = messageService;
    }

    @PostMapping("/add")
    @ApiOperation(value = "addTask", notes = "新建任务")
    public BaseResponse<Boolean> addTask(@RequestBody AddTaskRequest addTaskRequest) {
        ThrowUtils.throwIf(null == addTaskRequest, ErrorCode.PARAMS_ERROR);
        Boolean result = taskService.addTask(addTaskRequest);
        return ResultUtils.success(result);
    }

    @PutMapping("/update")
    @ApiOperation(value = "updateTask", notes = "更新任务")
    public BaseResponse<Boolean> updateTask(@RequestBody UpdateTaskRequest updateTaskRequest) {
        ThrowUtils.throwIf(null == updateTaskRequest, ErrorCode.PARAMS_ERROR);
        Boolean result = taskService.updateTask(updateTaskRequest);
        return ResultUtils.success(result);
    }

    @GetMapping
    @ApiOperation(value = "getTaskDetail", notes = "获取任务详情")
    public BaseResponse<TaskVo> getTaskDetail(Integer id) {
        ThrowUtils.throwIf(null == id, ErrorCode.PARAMS_ERROR);
        TaskVo taskVo = taskService.getDetail(id);
        return ResultUtils.success(taskVo);
    }

    @PostMapping("/page")
    @ApiOperation(value = "pageTasks", notes = "分页获取任务")
    public BaseResponse<Page<TaskVo>> pageTasks(@RequestBody PageTaskRequest pageTaskRequest) {
        ThrowUtils.throwIf(null == pageTaskRequest, ErrorCode.PARAMS_ERROR);
        Page<TaskVo> resultPage = taskService.pageTasks(pageTaskRequest);
        return ResultUtils.success(resultPage);
    }

    @DeleteMapping("/delete")
    @ApiOperation(value = "deleteTask", notes = "删除任务")
    public BaseResponse<Boolean> deleteTask(Integer taskId) {
        ThrowUtils.throwIf(null == taskId, ErrorCode.PARAMS_ERROR);
       boolean result = taskService.deleteTask(taskId);

        return ResultUtils.success(result);
    }

    @PostMapping("/dynamics/page")
    @ApiOperation(value = "pageTaskDynamics", notes = "分页获取任务动态")
    public BaseResponse<Page<TaskDynamicVo>> pageTaskDynamics(@RequestBody PageTaskDynamicRequest pageTaskDynamicRequest) {
        ThrowUtils.throwIf(null == pageTaskDynamicRequest, ErrorCode.PARAMS_ERROR);
        Page<TaskDynamicVo> resultPage = taskService.pageDynamics(pageTaskDynamicRequest);
        return ResultUtils.success(resultPage);
    }

    @PostMapping("/undoTaskList")
    @ApiOperation(value = "undoTaskList", notes = "获取我的未完成任务")
    public BaseResponse<List<TaskVo>> undoTaskList() {
        List<TaskVo> result = taskService.undoTaskList();
        return ResultUtils.success(result);
    }
}
